package com.ruoyi.member.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.vo.MemberInfoVo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.member.domain.bo.MemberInfoBo;
import com.ruoyi.member.service.IMemberInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 工作人员
 *
 * @author strongme
 * @date 2023-04-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/member/member")
public class MemberInfoController extends BaseController {

    private final IMemberInfoService iMemberInfoService;

    /**
     * 查询工作人员列表
     */
    @GetMapping("/list")
    public TableDataInfo<MemberInfoVo> list(MemberInfoBo bo, PageQuery pageQuery) {
        return iMemberInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工作人员列表
     */
    @Log(title = "工作人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MemberInfoBo bo, HttpServletResponse response) {
        List<MemberInfoVo> list = iMemberInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "工作人员", MemberInfoVo.class, response);
    }

    /**
     * 获取工作人员详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<MemberInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(iMemberInfoService.queryById(id));
    }

    /**
     * 新增工作人员
     */
    @Log(title = "工作人员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MemberInfoBo bo) {
        return toAjax(iMemberInfoService.insertByBo(bo));
    }

    /**
     * 修改工作人员
     */
    @Log(title = "工作人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MemberInfoBo bo) {
        return toAjax(iMemberInfoService.updateByBo(bo));
    }

    /**
     * 删除工作人员
     *
     * @param ids 主键串
     */
    @Log(title = "工作人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMemberInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
